//
//  GBPingHelper.m
//  PingTest
//
//  Created by zgy on 2025/4/16.
//

#import "GBPingHelper.h"

@interface GBPingHelper()<GBPingDelegate>


@property (nonatomic,   copy) void(^callback)(GBPingResType resType, GBPingSummary *pingSummary, NSError *error);

@end


@implementation GBPingHelper


+ (instancetype)ping:(NSString *)host timeout:(NSTimeInterval)timeout pingPeriod:(NSTimeInterval)pingPeriod callback:(void(^)(GBPingResType resType, GBPingSummary *pingSummary, NSError *error))callback
{
    GBPingHelper *helper = [GBPingHelper new];
    
    helper.callback = callback;
    
    helper.ping = [[GBPing alloc] init];
    helper.ping.host = host;
    helper.ping.delegate = helper;
    helper.ping.timeout = timeout;
    helper.ping.pingPeriod = pingPeriod;
    
    
    [helper.ping setupWithBlock:^(BOOL success, NSError *error) { //necessary to resolve hostname
        if (success) {
            // start pinging
            [helper.ping startPinging];
            
            // stop it after 5 seconds
            NSTimeInterval stopSecond = 5.f;
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(stopSecond * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                NSLog(@"Stop ping .");
                [helper.ping stop];
                helper.ping = nil;
            });
            
        }
        else {
            NSLog(@"failed to start");
            if (helper.callback) {
                helper.callback(GBPingResTypeFail, nil, error);
            }
        }
    }];
    
    return helper;
}


#pragma mark- GBPingDelegate
- (void)ping:(GBPing *)pinger didFailWithError:(NSError *)error {
//    NSLog(@"********* didFailWithError : %@", error);
    if (self.callback) {
        self.callback(GBPingResTypeFail, nil, error);
    }
}

- (void)ping:(GBPing *)pinger didSendPingWithSummary:(GBPingSummary *)summary {
//    NSLog(@"********* didSendPingWithSummary : %@", summary);
    
    if (self.callback) {
        self.callback(GBPingResTypeSend, summary, nil);
    }
}

- (void)ping:(GBPing *)pinger didFailToSendPingWithSummary:(GBPingSummary *)summary error:(NSError *)error {
//    NSLog(@"********* didFailToSendPingWithSummary : %@", error);
    
    if (self.callback) {
        self.callback(GBPingResTypeFailToSend, summary, error);
    }
}

- (void)ping:(GBPing *)pinger didTimeoutWithSummary:(GBPingSummary *)summary {
//    NSLog(@"********* didTimeoutWithSummary : %@", summary);
    
    if (self.callback) {
        self.callback(GBPingResTypeTimeout, summary, nil);
    }
}

- (void)ping:(GBPing *)pinger didReceiveReplyWithSummary:(GBPingSummary *)summary {
    
//    NSLog(@"********* didReceiveReplyWithSummary : %@", summary);
    
    if (self.callback) {
        self.callback(GBPingResTypeReceiveReply, summary, nil);
    }
}

- (void)ping:(GBPing *)pinger didReceiveUnexpectedReplyWithSummary:(GBPingSummary *)summary {
//    NSLog(@"********* didReceiveUnexpectedReplyWithSummary : %@", summary);
    
    if (self.callback) {
        self.callback(GBPingResTypeUnexpectedReply, summary, nil);
    }
    
}

@end
